package com.randi.system.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.randi.common.config.AppConfig;
import com.randi.common.constant.CacheConstants;
import com.randi.common.core.domain.entity.SysRole;
import com.randi.common.enums.ResultCode;
import com.randi.common.exception.ServiceException;
import com.randi.common.utils.SecurityUtils;
import com.randi.common.utils.page.PageHelperUtils;
import com.randi.system.domain.params.SysRoleParam;
import com.randi.system.mapper.SysRoleMapper;
import com.randi.system.service.ISysRoleService;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author ziye
 */
@Service
@CacheConfig(cacheNames = CacheConstants.SYS_ROLE)
public class SysRoleServiceImpl implements ISysRoleService {

    @Resource
    private SysRoleMapper sysRoleMapper;

    @Resource
    private AppConfig.SystemConfig systemConfig;

    /**
     * 分页查询业务列表
     *
     * @param sysRoleParam sysRoleParam
     * @return 业务集合
     */
    @Override
    @Cacheable(keyGenerator = "baseKeyGenerator", unless = "#result == null")
    public PageInfo<SysRole> selectSysRolePage(SysRoleParam sysRoleParam) {
        PageHelperUtils.initPage(sysRoleParam);
        List<SysRole> sysRolePageInfo = sysRoleMapper.selectSysRoleList(sysRoleParam);
        return new PageInfo<>(sysRolePageInfo);
    }

    /**
     * 查看详情
     *
     * @param roleId roleId
     * @return 业务集合
     */
    @Override
    @Cacheable(key = "#roleId", unless = "#result == null")
    public SysRole selectSysRoleById(Long roleId) {
        return sysRoleMapper.selectSysRoleById(roleId);
    }

    /**
     * 插入数据
     *
     * @param sysRole sysRole
     * @return 结果
     */
    @Override
    @Caching(evict = {
            @CacheEvict(allEntries = true, beforeInvocation = true),
            @CacheEvict(value = CacheConstants.SYS_ROLE_MENU, allEntries = true, beforeInvocation = true)
    })
    public int insertSysRole(SysRole sysRole) {
        SysRole role = new SysRole();
        role.setRoleName(sysRole.getRoleName());
        role.setRoleKey(sysRole.getRoleKey());
        role.setStatus(sysRole.getStatus());
        role.setRoleSort(sysRole.getRoleSort());
        role.setRemark(sysRole.getRemark());
        SecurityUtils.setInsertInfo(role);
        return sysRoleMapper.insert(role);
    }

    /**
     * 更新数据
     *
     * @param sysRole SysRole
     * @return 结果
     */
    @Override
    @Caching(evict = {
            @CacheEvict(allEntries = true, beforeInvocation = true),
            @CacheEvict(value = CacheConstants.SYS_ROLE_MENU, allEntries = true, beforeInvocation = true)
    })
    public int updateSysRole(SysRole sysRole) {
        Long roleAdminId = systemConfig.getRoleAdminId();
        if (sysRole.getRoleId().equals(roleAdminId)) {
            throw new ServiceException(ResultCode.BUSINESS_EDITOR_ERROR_DISABLE);
        }
        SecurityUtils.setUpdateInfo(sysRole);
        return sysRoleMapper.updateById(sysRole);
    }

    /**
     * 状态变更
     *
     * @param sysRole 业务信息
     * @return 结果
     */
    @Override
    @Caching(evict = {
            @CacheEvict(allEntries = true, beforeInvocation = true),
            @CacheEvict(value = CacheConstants.SYS_ROLE_MENU, allEntries = true, beforeInvocation = true)
    })
    public int updateSysRoleStatus(SysRole sysRole) {
        SysRole role = new SysRole();
        role.setRoleId(sysRole.getRoleId());
        role.setStatus(sysRole.getStatus());
        return sysRoleMapper.updateById(role);
    }

    /**
     * 批量删除数据
     *
     * @param roleIds roleIds
     * @return status
     */
    @Override
    @Caching(evict = {
            @CacheEvict(allEntries = true, beforeInvocation = true),
            @CacheEvict(value = CacheConstants.SYS_ROLE_MENU, allEntries = true, beforeInvocation = true)
    })
    public int deleteBatchSysRole(List<Long> roleIds) {
        return sysRoleMapper.batchUpdateDeleted(roleIds);
    }
}
